package com.hq.ims.data.job;

import com.hq.ims.data.alisms.AliSmsConfig;
import com.hq.ims.data.entity.BusForewarningSet;
import com.hq.ims.data.entity.BusInfoForewarning;
import com.hq.ims.data.entity.BusiInfo;
import com.hq.ims.data.entity.User;
import com.hq.ims.data.service.BaseBusForewarningSetService;
import com.hq.ims.data.service.BaseBusInfoForewarningService;
import com.hq.ims.data.service.BaseBusiInfoService;
import com.hq.ims.data.service.BaseUserService;
import com.hq.utils.util.JsonUtils;
import com.hq.utils.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cx
 * @date 2024/11/26 11:21:02
 * @description 预警定时任务-逻辑处理类
 */
@Component
@Slf4j
public class ForewarningJob implements Job {

    @Resource
    private AliSmsConfig aliSmsConfig;
    @Resource
    private BaseBusForewarningSetService baseBusForewarningSetService;
    @Resource
    private BaseBusInfoForewarningService baseBusInfoForewarningService;
    @Resource
    private BaseUserService baseUserService;
    @Resource
    private BaseBusiInfoService baseBusiInfoService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        log.info("=========================预警业务逻辑处理====================");
        log.info("任务名称jobName:{}", jobExecutionContext.getJobDetail().getKey().getName());
        log.info("任务参数:{}", jobExecutionContext.getMergedJobDataMap().get("params"));
        log.info("调度时间:{}", jobExecutionContext.getScheduledFireTime());
        // 预警id
        String busInfoForewarningId = jobExecutionContext.getMergedJobDataMap().getString("params");
        BusInfoForewarning busInfoForewarning = baseBusInfoForewarningService.getById(busInfoForewarningId);
        if (busInfoForewarning != null) {
            // 查询预警设置数据
            BusForewarningSet busForewarningSet = baseBusForewarningSetService.getByEnumFeedbackPhase(busInfoForewarning.getFeedbackPhase());
            // 短信code
            String smsCode = busForewarningSet.getSmsCode();
            if (StringUtils.isEmpty(smsCode)) {
                log.warn("短信code为空, 无法发送短信...");
                return;
            }
            String sendMessageUserId = busInfoForewarning.getSendMessageUserId();
            if (StringUtils.isEmpty(sendMessageUserId)) {
                log.warn("发送短信人为空, 无法发送短信...");
                return;
            }
            // 获取用户
            User user = baseUserService.getById(sendMessageUserId);
            // 查询项目
            BusiInfo busiInfo = baseBusiInfoService.getById(busInfoForewarning.getBusiInfoId());
            // 设置短信参数
            Map<String, String> param = new HashMap<>(1);
            param.put("projectName", busiInfo.getProjectName());
            String paramStr = JsonUtils.objectToString(param);
            log.info("开始发送短信...");
            boolean sendFlag = aliSmsConfig.sendSms(smsCode, user.getMobile(), paramStr);
            if (sendFlag) {
                // 发送成功后, 更新预警信息发送状态为已发送短信
                busInfoForewarning.setSendFlag(Boolean.TRUE);
                baseBusInfoForewarningService.updateById(busInfoForewarning);
            }
        }
    }
}
